﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Twi.COMMON.HTTP;
using System.Xml.Linq;
using Twi.COMMON.WPF;

namespace Twi.COMMON.WPF
{
    [ValueConversion(typeof(string), typeof(string))]
    public class NodeConverter : IValueConverter
    {
        private static List<XElement> ListNode = new List<XElement>();
        static NodeConverter() 
        {
            using (var res = "Node/GetMultiple".HttpGet())
            {
                ProcessRequest.Process(res, (ResponseMessageInfo resInfo) =>
                {
                    ListNode = resInfo.ResponseDataMainBody.Elements().ToList();
                });
            }
        }

        /// <summary>
        /// 获取用于客户端显示的流程节点信息字典
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetClientFlowNodeDic(string flowCode)
        {
            Dictionary<string, string> dicNode = new Dictionary<string, string>();
            foreach (XElement xeNode in ListNode.Where(x => x.Element("FlowCode").GetValue() == flowCode && x.Element("ClientVisibility").GetValue() == "0"))
            {
                if (xeNode.Element("NodeFlag").GetValue<int>(0) == 1)
                {
                    dicNode.Add(xeNode.Element("NodeCode").Value, xeNode.Element("ClientDisplayName").Value);
                }
            }
            return dicNode;
        }

        /// <summary>
        /// NodeFlag >= 1
        /// </summary>
        /// <returns></returns>
        //public static Dictionary<string, string> GetDicNode2(string flowCode)
        //{
        //    Dictionary<string, string> dicNode = new Dictionary<string, string>();
        //    foreach (XElement xeNode in ListNode.Where(x => x.Element("FlowCode").GetValue() == flowCode))
        //    {
        //        if (xeNode.Element("NodeFlag").GetValue<int>(0) >= 1)
        //        {
        //            dicNode.Add(xeNode.Element("NodeCode").Value, xeNode.Element("NodeName").Value);
        //        }
        //    }
        //    return dicNode;
        //}
        
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (parameter == null)
            {
                XElement xeNode = ListNode.FirstOrDefault(x => x.Element("NodeCode").Value == value.ToString());
                return string.Format("【{0}】", xeNode != null ? xeNode.Element("NodeName").Value : "");
            }
            else
            {
                return string.Format("【{0}】", value);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
